package com.red.servlet.user;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.red.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

public class UserDeleteServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        JSONObject json = JSONObject.parseObject(req.getParameter("target"));
        JSONArray jsonArray = json.getJSONArray("target");

        ArrayList<Integer> seq = new ArrayList<>();

        for (int i = 0; i < jsonArray.size(); i++) {
            seq.add(jsonArray.getIntValue(i));
        }

        int result = new UserServiceImpl().delete(seq);
        String info = "";
        if (result == 0) {
            info = "删除失败，或没有选择任何用户";
        } else {
            info = String.format("删除了 %d 个用户", result);
        }
        req.setAttribute("deleteResult", info);
        req.getRequestDispatcher("user_manage").forward(req, resp);
    }
}
